package com.example.demo.javatest.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * Created by liujiahao on 2019/3/1.
 */
public class ThreadTest {
    public static void main(String[] args) throws Exception {
        Thread a = new Thread(new Runnable() {
            @Override
            public void run() {
                testMethod();
            }
        }, "a");
        Thread b = new Thread(new Runnable() {
            @Override
            public void run() {
                testMethod();
            }
        }, "b");
        a.start();
        a.join();
        b.start();
        b.join();
        System.out.println( "main:" + Thread.currentThread().getState());
        System.out.println(a.getName() + ":" + a.getState()); // 输出？
        System.out.println(b.getName() + ":" + b.getState()); // 输出？

    }

    // 同步方法争夺锁
    private synchronized static void testMethod() {
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void fun1(){
        Mythread2 t=new Mythread2();
        t.setName("t1");
        t.start();
        try {
            t.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t.interrupt();
    }
    public static void  fun2(){
        Service s=new Service();
        ThreadA a=new ThreadA(s);
        a.setName("a");
        a.start();
        ThreadB b=new ThreadB(s);
        b.setName("b");
        b.start();
    }
}
